import glob
import os

import pandas as pd

from utils.file_utils import get_file_name_and_extension

if __name__ == '__main__':
    _data = 'result'
    _result = '拆分'
    _file_list = glob.glob(f'{_data}/*.xlsx')
    for _file in _file_list:
        dir_path, file_name, extension = get_file_name_and_extension(_file)
        _df = pd.read_excel(_file)
        for _city_name, _city_group_df in _df.groupby(['city']):
            _save_path = os.path.join(_result, _city_name)
            if not os.path.exists(_save_path):
                os.makedirs(_save_path, exist_ok=True)
            _city_group_df.to_excel(os.path.join(_save_path, f'{file_name}.xlsx'), index=False)
